﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.VirtualTeam.Impl;
using BLL.VirtualTeam;
using Model;
using GenericLibrariesToolKit;
using System.Text;
using Utils;

public partial class Page_RFP_RFPList : System.Web.UI.Page
{
    RFPBO rfpBO = new RFPBOImpl();
    protected void Page_Load(object sender, EventArgs e)
    {

            RFPDataBind();
            UIControl();

    }
    protected void btnPostRFPSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string RFPTitle = txtRFPTitle.Text.Trim();
        string RFPDeatil = txtPostRFPSummary.Text.Trim();
        string RFPType = ddlPostRFPCategory.Text.Trim();
        RFP rfp = new RFP() 
        {
            RFPTitle=RFPTitle,
            ExpertId = WebUtil.GetIdentifier(Session["userId"]),
            RFPType=RFPType,
            RFPDetail=RFPDeatil
        };
       
        bool result = rfpBO.Add(rfp);
        if (result)
        {
            Response.Write("<script language=javascript>alert('success！')</script>");
            Response.Write("<script language=javascript>window.location.href='RFPList.aspx?type=myRFP'</script>");
        }
        else
        {
            Response.Write("<script>alert('RFP post fail')</script>");
 
        }
    }
    protected void LBRFPTitle_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string RFPId = btn.CommandArgument.ToString();
        Response.Redirect("RFPDetails.aspx?RFPId=" + RFPId+"&type=detail");
    }
    protected void LBRFPPoster_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string ExpertId = btn.CommandArgument.ToString();
        Response.Redirect(WebUtil.GetCurrentPath(Request.Url)+"Expert/ExpertDetails.aspx?expertId="+ExpertId);
    }

    //RFP列表绑定
    private void RFPDataBind()
    {
        //设置orderby查询条件
        Dictionary<string, string> orderBy = new Dictionary<string, string>();
        orderBy.Add("RFPDate", "desc");
        string type = Request["type"].ToString();
        List<RFP> listRFPs = new List<RFP>();
        if (type == "allRFP")
        {
            QueryResult<RFP> queryRFPs = rfpBO.GetScrollData(-1, -1, null, orderBy);
            listRFPs = queryRFPs.ResultList;
        }
        else if (type == "myRFP")
        {
            listRFPs = rfpBO.GetEntityList("ExpertId", WebUtil.GetIdentifier(Session["userId"]),"RFPDate");
        }
        GVRFP.DataSource = listRFPs;
        GVRFP.DataBind();
    }

    private void UIControl()
    {
        string type = Request["type"].ToString();
        if (type == "allRFP"||type == "myRFP")
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script>");
            script.Append("document.getElementById('PostRFP').style.display='none';");
            script.Append("</script>");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
        }
        else if (type == "postRFP")
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script>");
            script.Append("document.getElementById('RFPList').style.display='none';");
            script.Append("</script>");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());

        }
    }
    //分页处理
    protected void GVRFP_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GVRFP.PageIndex = e.NewPageIndex;
        this.GVRFP.DataBind();
    }

    protected void btnRFPBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
}